package com.walkline.app;

import java.util.Vector;

import net.rim.device.api.io.transport.TransportInfo;
import net.rim.device.api.system.PersistentObject;
import net.rim.device.api.system.PersistentStore;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Ui;
import net.rim.device.api.ui.XYEdges;
import net.rim.device.api.ui.decor.Background;
import net.rim.device.api.ui.decor.BackgroundFactory;
import net.rim.device.api.ui.decor.Border;
import net.rim.device.api.ui.decor.BorderFactory;
import net.rim.device.api.util.Persistable;

import com.walkline.util.Enumerations.AppSource;
import com.walkline.util.Enumerations.RefreshRemindDelay;
import com.walkline.util.Enumerations.RepostCount;
import com.walkline.util.Enumerations.ShortcutKey;
import com.walkline.util.Enumerations.ShowStatusImage;
import com.walkline.util.Enumerations.StatusCount;
import com.walkline.util.Enumerations.StorageType;
import com.walkline.util.Enumerations.SuggestionCount;
import com.walkline.util.Enumerations.Themes;
import com.walkline.weibo.WeiboSDK;

public class WeiboAppConfig implements Persistable
{
	public static final String APP_NAME = "Another Veibo for ";
	public static final String APP_TITLE = "Another Veibo for you";
	public static final String UNDERLINE = "\u0332";
	public static final String BBW_APPID = "59946480";
	//blackberry_another_weibo_written_by_Walkline_Wang

	public static int[] preferredTransportTypes = {TransportInfo.TRANSPORT_TCP_WIFI, TransportInfo.TRANSPORT_BIS_B, TransportInfo.TRANSPORT_TCP_CELLULAR, TransportInfo.TRANSPORT_WAP2};
	public static int[] disallowedTransportTypes = {TransportInfo.TRANSPORT_MDS, TransportInfo.TRANSPORT_WAP};

	public static final Background bgColor_Gradient=BackgroundFactory.createLinearGradientBackground(Color.GRAY, Color.GRAY, Color.BLACK, Color.BLACK);
	public static final Background bgColor_GradientBlue=BackgroundFactory.createLinearGradientBackground(0x4992ce, 0x4992ce, 0x05549d, 0x05549d);
	public static final Border border_Transparent=BorderFactory.createRoundedBorder(new XYEdges(16,16,16,16), Color.BLACK, 0, Border.STYLE_FILLED);
	public static final Background bg_Transparent=BackgroundFactory.createSolidTransparentBackground(Color.BLACK, 0);
	public static final Border border_popup_Transparent=BorderFactory.createRoundedBorder(new XYEdges(16,16,16,16), Color.BLACK, 200, Border.STYLE_FILLED);
	public static final Background bg_popup_Transparent=BackgroundFactory.createSolidTransparentBackground(Color.BLACK, 200);

	public static final Font FONT_LIST_TITLE = Font.getDefault().derive(Font.PLAIN, Font.getDefault().getHeight(Ui.UNITS_pt) + 2, Ui.UNITS_pt);
	public static final Font FONT_LIST_DESCRIPTION = Font.getDefault().derive(Font.PLAIN, Font.getDefault().getHeight(Ui.UNITS_pt)-1, Ui.UNITS_pt);
	//public static final Font FONT_DATE_NORMAL = Font.getDefault().derive(Font.PLAIN, Font.getDefault().getHeight(Ui.UNITS_pt)-1, Ui.UNITS_pt);
	
	public static final Font FONT_SETTINGS_TITLE = Font.getDefault().derive(Font.PLAIN, Font.getDefault().getHeight(Ui.UNITS_pt)-1, Ui.UNITS_pt);
	public static final Font FONT_SETTINGS_DESCRIPTION = Font.getDefault().derive(Font.ITALIC, Font.getDefault().getHeight(Ui.UNITS_pt)-1, Ui.UNITS_pt);

	public static final Font FONT_ABOUT_TITLE = Font.getDefault().derive(Font.BOLD, Font.getDefault().getHeight(Ui.UNITS_pt) + 2, Ui.UNITS_pt);
	public static final Font FONT_ABOUT_HEADLINE = Font.getDefault().derive(Font.BOLD | Font.ITALIC, Font.getDefault().getHeight(Ui.UNITS_pt), Ui.UNITS_pt);
	public static final Font FONT_ABOUT_SMALL = Font.getDefault().derive(Font.PLAIN, Font.getDefault().getHeight(Ui.UNITS_pt) - 1, Ui.UNITS_pt);
	public static final Font FONT_ABOUT_LARGE = Font.getDefault().derive(Font.PLAIN, Font.getDefault().getHeight(Ui.UNITS_pt) + 1, Ui.UNITS_pt);

	public static final Font FONT_MAIN_TITLE = Font.getDefault().derive(Font.PLAIN, Font.getDefault().getHeight(Ui.UNITS_pt) + 1, Ui.UNITS_pt);

	public static final Font FONT_SCREENNAME = Font.getDefault().derive(Font.PLAIN, Font.getDefault().getHeight(Ui.UNITS_pt) + 1,Ui.UNITS_pt);
	public static final Font FONT_SOURCE = Font.getDefault().derive(Font.PLAIN, Font.getDefault().getHeight(Ui.UNITS_pt) - 2, Ui.UNITS_pt);
	public static final Font FONT_TIME = Font.getDefault().derive(Font.PLAIN, Font.getDefault().getHeight(Ui.UNITS_pt) - 1, Ui.UNITS_pt);

	//Elements and Keys
	private Vector _elements;
	private static final int AUTO_LOGIN = 0;
	private static final int THEMES = 1;
	private static final int STATUS_COUNT = 2;
	private static final int REPOST_COUNT = 3;
	private static final int SUGGESTION_COUNT = 4;
	private static final int AUTO_LOAD_STATUS_IMAGE = 5;
	private static final int APP_SOURCE = 6;
	private static final int OVERWRITE = 7;
	private static final int USERNAME = 8;
	private static final int PASSWORD = 9;
	private static final int SHORTCUT_KEY = 10;
	private static final int DOWNLOAD_LOCATION = 11;
	private static final int ACCESS_TOKEN = 12;
	private static final int REFRESH_REMIND_DELAY = 13;
	private static final int EXPIRE_TIME_MILLIS = 14;

	//Persistent objects
	private static Vector _data;
	private static PersistentObject _store;

	private boolean _autoMode = true;
	private boolean _overWrite = true;
	private int _themes = Themes.DEFAULT_THTMES;
	private int _statusCount = StatusCount.DEFAULT_COUNT;
	private int _repostCount = RepostCount.DEFAULT_COUNT;
	private int _suggestionCount = SuggestionCount.DEFAULT_COUNT;
	private int _refreshRemindDelay = RefreshRemindDelay.DEFAULT_DELAY;
	private int _autoLoadStatusImage = ShowStatusImage.DEFAULT_OPTION;
	private int _downloadLocation = StorageType.DEFAULT_TYPE;
	private int _appSource = AppSource.DEFAULT_SOURCE;
	private int _shortcutKey = ShortcutKey.DEFAULT_KEY;
	private String _username = "";
	private String _password = "";
	private String _accessToken = "";
	private long _expireTimeMillis = 0;

	public WeiboAppConfig()
	{
		_elements = new Vector(15);

		for (int i=0; i<_elements.capacity(); i++)
		{
			_elements.addElement(new Object());
		}
	}

	private boolean getElementBoolean(int id)
	{
		boolean result = false;
		Object valueObject = _elements.elementAt(id); 

		if (valueObject instanceof Boolean)
		{
			result = ((Boolean) valueObject).booleanValue();
		}

		return result;
	}

	private int getElementInt(int id) {return Integer.parseInt(_elements.elementAt(id).toString());}

	private long getElementLong(int id) {return Long.parseLong(_elements.elementAt(id).toString());}

	private String getElementString(int id) {return (String) _elements.elementAt(id);}

	private void setElement(int id, Object value) {_elements.setElementAt(value, id);}

	public void setAutoMode(boolean value)
	{
		setElement(AUTO_LOGIN, new Boolean(value));
		_autoMode = value;
	}

	public boolean isAutoMode() {return _autoMode;}

	public void setOverwrite(boolean value)
	{
		setElement(OVERWRITE, new Boolean(value));
		_overWrite = value;
	}

	public boolean isOverwrite() {return _overWrite;}

	public void setDownloadLocation(int value)
	{
		setElement(DOWNLOAD_LOCATION, new Integer(value));
		_downloadLocation = value;
	}

	public int getDownloadLocation() {return _downloadLocation;}

	public void setThemes(int value)
	{
		setElement(THEMES, new Integer(value));
		_themes = value;
	}

	public int getThemes() {return _themes;}

	public void setStatusCount(int value)
	{
		setElement(STATUS_COUNT, new Integer(value));
		_statusCount = value;
	}

	public int getStatusCount() {return _statusCount;}

	public void setRepostCount(int value)
	{
		setElement(REPOST_COUNT, new Integer(value));
		_repostCount = value;
	}

	public int getRepostCount() {return _repostCount;}

	public void setSuggestionCount(int value)
	{
		setElement(SUGGESTION_COUNT, new Integer(value));
		_suggestionCount = value;
	}

	public int getSuggestionCount() {return _suggestionCount;}

	public void setRemindDelay(int value)
	{
		setElement(REFRESH_REMIND_DELAY, new Integer(value));
		_refreshRemindDelay = value;
	}

	public int getRemindDelay() {return _refreshRemindDelay;}

	public void setAutoLoadImageMethod(int value)
	{
		setElement(AUTO_LOAD_STATUS_IMAGE, new Integer(value));
		_autoLoadStatusImage = value;
	}

	public int getAutoLoadImageMethod() {return _autoLoadStatusImage;}

	public void setAppSource(int value)
	{
		setElement(APP_SOURCE, new Integer(value));
		_appSource = value;
	}

	public int getAppSource() {return _appSource;}

	public void setShortcutKey(int value)
	{
		setElement(SHORTCUT_KEY, new Integer(value));
		_shortcutKey = value;
	}

	public int getShortcutKey() {return _shortcutKey;}

	public void setUsername(String value)
	{
		setElement(USERNAME, new String(value));
		_username = value;
	}

	public String getUsername() {return _username;}

	public void setAccessToken(String value)
	{
		setElement(ACCESS_TOKEN, new String(value));
		_accessToken = value;
	}

	public String getAccessToken() {return _accessToken;}

	public void setExpireTimeMillis(long value)
	{
		setElement(EXPIRE_TIME_MILLIS, new Long(value));
		_expireTimeMillis = value;
	}

	public long getExpireTimeMillis() {return _expireTimeMillis;}

	public void setPassword(String value)
	{
		setElement(PASSWORD, new String(value));
		_password = value;
	}

	public String getPassword() {return _password;}

	public boolean isAccountEmpty()
	{
		boolean result = false;

		if (getUsername().equals("") || getUsername().length() < 0 || getPassword().equals("") || getPassword().length() < 0)
		{
			result = true;
		}

		return result;
	}

	public void clean()
	{
		synchronized (_store)
		{
			_store.setContents(new Vector());
			_store.forceCommit();

			return;
		}
	}

	public void initialize(WeiboSDK weibo)
	{
		synchronized(_store)
		{
			try {
				_data = (Vector) _store.getContents();

				if (!_data.isEmpty())
				{
					_elements = (Vector) _data.lastElement();

					setAutoMode(getElementBoolean(AUTO_LOGIN));
					setUsername(getElementString(USERNAME));
					setPassword(getElementString(PASSWORD));

					setOverwrite(getElementBoolean(OVERWRITE));
					setThemes(getElementInt(THEMES));

					setStatusCount(getElementInt(STATUS_COUNT));
					//setRepostCount(getElementInt(REPOST_COUNT));
					setSuggestionCount(getElementInt(SUGGESTION_COUNT));
					setRemindDelay(getElementInt(REFRESH_REMIND_DELAY));

					setAutoLoadImageMethod(getElementInt(AUTO_LOAD_STATUS_IMAGE));
					setDownloadLocation(getElementInt(DOWNLOAD_LOCATION));
					setAppSource(getElementInt(APP_SOURCE));

					setAccessToken(getElementString(ACCESS_TOKEN));
					setExpireTimeMillis(getElementLong(EXPIRE_TIME_MILLIS));

					_shortcutKey = getElementInt(SHORTCUT_KEY);

					if (weibo != null)
					{
						weibo.setAutoMode(isAutoMode(), getUsername(), getPassword());
						weibo.setAccessToken(getAccessToken());
						weibo.setExpireTimeMillis(getExpireTimeMillis());
						weibo.setAppSource(getAppSource());
					}
				} else{
					makeDefaultSettings(weibo);
				}
			} catch (Exception e) {
				_store.setContents(new Vector());
				_store.forceCommit();
				_data = new Vector();

				makeDefaultSettings(weibo);
			}
		}
	}

	public void save(WeiboSDK _weibo, boolean updateSDK)
	{
		setAutoMode(isAutoMode());
		setUsername(getUsername());
		setPassword(getPassword());

		setOverwrite(_overWrite);
		setThemes(getThemes());

		setStatusCount(getStatusCount());
		//setRepostCount(getRepostCount());
		setSuggestionCount(getSuggestionCount());
		setRemindDelay(getRemindDelay());

		setAutoLoadImageMethod(getAutoLoadImageMethod());
		setDownloadLocation(_downloadLocation);
		setAppSource(getAppSource());
		setShortcutKey(_shortcutKey);

		setAccessToken(getAccessToken());
		setExpireTimeMillis(getExpireTimeMillis());

		_data.addElement(_elements);

		synchronized(_store)
		{
			_store.setContents(_data);
			_store.commit();
		}

		if (updateSDK && _weibo != null)
		{
			_weibo.setAutoMode(isAutoMode(), getUsername(), getPassword());
			_weibo.setAccessToken(getAccessToken());
			_weibo.setExpireTimeMillis(getExpireTimeMillis());
			_weibo.setAppSource(getAppSource());
		}
	}

	private void makeDefaultSettings(WeiboSDK weibo)
	{
		setAutoMode(true);
		setUsername("");
		setPassword("");

		setOverwrite(true);
		setShortcutKey(ShortcutKey.DEFAULT_KEY);

		setAccessToken("");
		setExpireTimeMillis(0);

		setStatusCount(StatusCount.DEFAULT_COUNT);
		//setRepostCount(RepostCount.DEFAULT_COUNT);
		setSuggestionCount(SuggestionCount.DEFAULT_COUNT);
		setRemindDelay(RefreshRemindDelay.DEFAULT_DELAY);

		setDownloadLocation(StorageType.DEFAULT_TYPE);
		setThemes(Themes.DEFAULT_THTMES);
		setAutoLoadImageMethod(ShowStatusImage.DEFAULT_OPTION);
		setAppSource(AppSource.DEFAULT_SOURCE);

		_data.addElement(_elements);

		synchronized(_store)
		{
			_store.setContents(_data);
			_store.forceCommit();
		}

		if (weibo != null)
		{
			weibo.setAutoMode(true, "", "");
			weibo.setAccessToken("");
			weibo.setExpireTimeMillis(0);
			weibo.setAppSource(AppSource.DEFAULT_SOURCE);
		}
	}

	static
	{
		_store = PersistentStore.getPersistentObject(0x3639bd9a1bece9bbL); //blackberry_another_weibo_written_by_Walkline_Wang
	}
}